package com.heima.article.interceptor;

import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.ApuserThreadLocalUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2022/11/10 9:37
 * @Version 1.0
 */
public class ApUserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("userId");
        if(!StringUtils.isEmpty(userId)){
            //存用户数据
            ApUser apUser = new ApUser();
            apUser.setId(Integer.parseInt(userId));
            ApuserThreadLocalUtil.set(apUser);
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ApuserThreadLocalUtil.clear();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}